/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: wx
 * Date: 2023-09-14
 * Time: 0:37
 */
public class MySingleLinkedList implements MyLinkedList {

    static class SingleLink {
        public int val;
        public SingleLink next;

        public SingleLink(int val) {
            this.val = val;
        }
    }

    public SingleLink haed;

    public void MySingleLink() {
        SingleLink demo1 = new SingleLink(12);
        SingleLink demo2 = new SingleLink(23);
        SingleLink demo3 = new SingleLink(34);
        SingleLink demo4 = new SingleLink(45);
        SingleLink demo5 = new SingleLink(56);

        demo1.next = demo2;
        demo2.next = demo3;
        demo3.next = demo4;
        demo5.next = demo4;

        this.haed = demo1;
    }

    @Override
    public void addFirst(int data) {
        SingleLink cur = this.haed;
        SingleLink singleLink = new SingleLink(data);
        singleLink.next = this.haed;
        this.haed = singleLink;
    }

    @Override
    public void addLast(int data) {
        SingleLink cur = this.haed;
        SingleLink se1 = new SingleLink(data);
        if (cur == null) {
            this.haed = se1;
            return;
        }
        while (cur.next != null) {
            cur = cur.next;
        }
        cur = se1;
    }

    @Override
    public void addIndex(int index, int data) {
        if (index < 0 || index > size()) {
            //跑一个自定义异常
            return;
        }
        if (index == 0) {
            addFirst(data);
            return;
        }
        if (index == size()) {
            addLast(data);
            return;
        }
        SingleLink cur = indexfind(index);
        SingleLink niu = new SingleLink(data);

        niu.next = cur.next;
        cur.next = niu;


    }

    private SingleLink indexfind(int index) {
        SingleLink cur = this.haed;
        int count = 0;
        while (count != index - 1) {
            cur = cur.next;
            count++;
        }
        return cur;
    }

    @Override
    public boolean contains(int key) {

        SingleLink cur = this.haed;

        if (cur == null) {
            return false;
        }
        while (cur.next != null) {
            if (cur.val == key) {
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    @Override
    public void remove(int key) {

    }

    @Override
    public void removeAllKey(int key) {

    }

    @Override
    public int size() {
        SingleLink cur = this.haed;
        int count = 0;
        while (cur != null) {
            count++;
            cur = cur.next;
        }
        return count;
    }

    @Override
    public void clear() {

        SingleLink cur = this.haed;
        while (cur != null) {
            SingleLink curNext = cur.next;
            cur = null;
            cur.next = null;
            cur = cur.next;
            cur = curNext;
        }
        haed = null;
    }

    @Override
    public void display() {
        SingleLink cur = this.haed;

        while (cur != null) {
            System.out.println(haed.val + " ");
            cur = cur.next;
        }

    }

    public void reverseList() {
        if (haed == null) {
            return null;
        }
//只有一个节点
        if (haed.next == null) {
            return haed;
        }
//
        SingleLink cur = haed.next;

        haed.next = null;
        while (cur != null) {
            SingleLink heatNext = cur.next;
            SingleLink singleLink = new SingleLink(cur.val);
            singleLink.next = this.haed;
            this.haed = singleLink;
            cur = heatNext;
        }

      /*  public ListNode middleNode() {

            listNode cur = this.head;
            int count = 0;
            while (cur.next!=null) {
                count++;
                cur = cur.next;
            }
            cur = this.head;

        for(int i = 0;cur <= count/2;i++) {
            cur = cur .next;
    }
        return cur;
}

    public ListNode findKthToTail(int k) {
        if(k <= 0 || k > size()) {
            return null;
        }
        ListNode fast = head;
        ListNode slow = head;


    }
    public MySingleList.ListNode mergeTwoLists(MySingleList.ListNode head1,
                                               MySingleList.ListNode head2) {
        MySingleList.ListNode newH = new MySingleList.ListNode(-1);
        MySingleList.ListNode tmpH = newH;

        while (head1 != null && head2 != null) {
            if(head1.val < head2.val) {
                newH.next = head2.val;
            }else {
                newH.next = head1.val;
            }
            head1 = head1.next;
            head2 = head2.next;

        }

        while (head1 != null) {
            tmpH.next = head1;
        }

        while (head2 != null) {
            tmpH.next = head2;
        }

        return newH.next;
}*/
    }
}